﻿using AntdUI;

namespace WenAntdUI.Controls
{
    [ToolboxItem(true)]
    public class UserInfo : IControl
    {
        public UserInfo() : base()
        {
            this.BackColor = Color.Transparent;
        }

        [Description("默认图标")]
        public string IconSvg { get; set; } = "UserOutlined";

        [Description("图档")]
        public Image? Image { get; set; }

        [Description("用户名称")]
        public string? DisplayName { get; set; }

        [Description("图标尺寸")]
        public Size IconSize { get; set; } = new Size(32, 32);

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            var g = e.Graphics.High();

            var y = (this.Height - IconSize.Height) / 2;
            var imgRec = new Rectangle(3, y, IconSize.Width, IconSize.Height);

            if (Image != null)
            {
                g.Image(imgRec, this.Image, TFit.Cover, 10, TShape.Round);
            }
            else
            {
                g.Image(SvgExtend.GetImgExtend(IconSvg, imgRec), imgRec);
            }

            g.Draw(Color.Azure, 2, imgRec.RoundPath(10));

            if (!string.IsNullOrEmpty(DisplayName))
            {
                var x = imgRec.X + imgRec.Width + 3;
                var rec = new RectangleF(x, 3, this.Width - x - 3, this.Height - 6);
                g.String(DisplayName, this.Font, this.ForeColor, rec, new StringFormat(StringFormatFlags.NoClip | StringFormatFlags.NoWrap)
                {
                    LineAlignment = StringAlignment.Center,
                    Trimming = StringTrimming.EllipsisCharacter
                });
            }
        }
    }
}
